package com.msb.servlet;

import com.msb.entity.Cpi;
import com.msb.mapper.Mapper;
import com.msb.unitil.DBUnitil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @version : 1.0
 * @Author: yangmingsong
 * @Date: 2021/2/7-02-07-23:30
 * @Description: demon
 */
@WebServlet(urlPatterns = "/Doupdate")
public class Doupdate extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、设置字符编码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
//2、获取表单数据
        Cpi cpi = new Cpi();
        String dataType = request.getParameter("dataType");
        cpi.setDataType(dataType);
        String area = request.getParameter("area");
        cpi.setArea(area);
        String date = request.getParameter("date");
        cpi.setDate(date);
        String goodsName = request.getParameter("goodsName");
        cpi.setGoodsName(goodsName);
        Double price = Double.valueOf(request.getParameter("price"));
        cpi.setPrice(price);
        String unit = request.getParameter("unit");
        cpi.setUnit(unit);
        String remark = request.getParameter("remark");
        cpi.setRemark(remark);
//3、数据校验
//4、调用DBUtils添加方法
        Mapper mapper = DBUnitil.getsqlsession().getMapper(Mapper.class);
        Integer i = mapper.updata(cpi);
        DBUnitil.getsqlsession().close();
//5、处理返回结果
        if(i > 0){
            //重定向，实现页面跳转
            request.setAttribute("msg1","修改成功");
            request.getRequestDispatcher("home.jsp").forward(request,response);

        }else{
            //提示信息
            request.setAttribute("msg","修改失败");
            //重定向实现页面跳转
            request.getRequestDispatcher("list.jsp").forward(request,response);
        }
    }
}
